cc.Class({
    extends: cc.Component,

    properties: {
        msgPrefab: cc.Prefab,
        content: cc.Node,
        msgInput: cc.Label,
        nameInput: cc.Label,
    },

    onLoad() {},

    start() {
        this.ws = new WebSocket("ws://192.168.2.14:3000");
        this.ws.onmessage = (event) => {
            let data = JSON.parse(event.data);
            let msgNode = cc.instantiate(this.msgPrefab);
            msgNode.getComponent(
                cc.Label
            ).string = `${data.username}：${data.msg}`;
            this.content.addChild(msgNode);
        };
    },
    onBtnSendClick() {
        let msg = this.msgInput.string;
        this.msgInput.string = "";
        this.ws.send(
            JSON.stringify({ username: this.nameInput.string, msg: msg })
        );
    },

    // update (dt) {},
});
